﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DevExpress.Utils;

namespace Extensions
{
    public static class GenericExtensions
    {
        public static DataTable ToDataTable<T>(this List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);
            //Get all the properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                //Setting column names as Property names
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    //inserting property values to datatable rows
                    values[i] = Props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
            //put a breakpoint here and check datatable
            return dataTable;
        }

        public static Bitmap ToBitmap(this byte[] bytes)
        {
            Bitmap bitmap = null;
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                bitmap= (Bitmap)Image.FromStream(ms);
            }
            return bitmap;
        }

        public static decimal ToDecimalNN(this object value)
        {
            decimal result = -1;

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                try
                {
                    CultureInfo ci = new CultureInfo("en-US");
                    result = Convert.ToDecimal(value,ci);
                }
                catch
                {

                }
            }
            return result;
        }

        public static int ToIntNN(this object value)
        {
            int result = -1;
            
            if (value == DBNull.Value)
            {                
            }
            else if (value == null || value.ToString() == "")
            {                
            }
            else
            {
                try
                {
                    result = Convert.ToInt32(value);
                }
                catch 
                {                    
                }
            }
            return result;
        }

        public static int ToIntZero(this object value)
        {
            int result = 0;

            if (value == DBNull.Value)
            {
            }
            else if (value == null || value.ToString() == "")
            {
            }
            else
            {
                try
                {
                    result = Convert.ToInt32(value);
                }
                catch
                {
                }
            }
            return result;
        }

        public static long ToBigIntNN(this object value)
        {
            long result = -1;

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                try
                {
                    result = Convert.ToInt64(value);
                }
                catch
                {
                }
            }
            return result;
        }

        /// <summary>
        /// Trys to return string, if fails returns empty string.
        /// </summary>
        /// <param name="value">Source string</param>
        /// <returns>Result string</returns>
        public static string ToStringNN(this object value)
        {
            string result = "";

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                result = value.ToString();
            }
            return result;
        }

        public static bool ToBool(this DefaultBoolean value)
        {
            return value == DefaultBoolean.True ? true : false;
        }

        public static DefaultBoolean Toggle(this DefaultBoolean value)
        {
            if (value == DefaultBoolean.True)
                return DefaultBoolean.False;
            else if (value == DefaultBoolean.False)
                return DefaultBoolean.True;
            else
                return DefaultBoolean.Default;
        }

        public static string RemoveBetween(this string source, char begin, char end)
        {
            Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end));
            return regex.Replace(source, string.Empty);
        }

        public static string RemoveIfEndsWith(this string source, string stringToRemove)
        {
             if (source.EndsWith(stringToRemove))
                 source = source.Substring(0, source.Length-stringToRemove.Length);

             return source;
        }
    }

    public static class BindingSourceExtensions
    {
        public static object GetCurrentValue(this BindingSource bindingSource,string ColumnName)
        {
            object result = null;
            if (bindingSource != null && bindingSource.Current != null)            
            {
                result = ((DataRowView)bindingSource.Current).Row[ColumnName];
            }
              
            return result;
        }
    }
}